﻿using Spire.Pdf;
using Spire.Presentation;
using System;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ExcelTest("cc.xls");
            ExcelTest("cc.xlsx");

            WordTest("cc.doc");
            WordTest("cc.docx");

            PdfTest("cc.pdf");

            PptTest("cc.pptx");
            PptTest("cc.ppt");
            Console.WriteLine("Hello World!");
        }

        static void ExcelTest(string file)
        {
            var workbook = new Spire.Xls.Workbook();

            workbook.LoadFromFile(file);

            var sb = new StringBuilder();
            
            foreach(var sheet in workbook.Worksheets)
            {
                var rows = sheet.Rows.Length;
                var cols = sheet.Columns.Length;

                for(var curRow = 0; curRow < rows; curRow++)
                {
                    for(var curCol = 0; curCol < cols; curCol++)
                    {
                        
                        var str = sheet.Range[curRow+1, curCol+1].Text;
                        sb.Append(str);
                        sb.Append("\t");
                    }
                    sb.AppendLine();
                }
                sb.AppendLine();
                sb.AppendLine();
            }

            var text = sb.ToString();
        }

        static void WordTest(string file)
        {
            var doc = new Spire.Doc.Document(file);

            var warningStr = "Evaluation Warning: The document was created with Spire.Doc for .NET.";

            var text = doc.GetText();

            if (text.StartsWith(warningStr))
            {
                text = text.Substring(warningStr.Length);
            }
        }

        static void PdfTest(string file)
        {
            var pdf = new Spire.Pdf.PdfDocument(file);

            //实例化一个StringBuilder 对象
            StringBuilder content = new StringBuilder();

            //提取PDF所有页面的文本
            foreach (PdfPageBase page in pdf.Pages)
            {
                var item = page.ExtractText();

                var warningStr = "Evaluation Warning : The document was created with Spire.PDF for .NET. ";
                if (item.StartsWith(warningStr))
                {
                    item = item.Substring(warningStr.Length);
                }

                content.Append(item);
            }

            var text = content.ToString();
        }

        static void PptTest(string file)
        {
            var ppt = new Spire.Presentation.Presentation(file, Spire.Presentation.FileFormat.Auto);

            //实例化一个StringBuilder 对象
            var sb = new StringBuilder();

            //提取PPT所有页面的文本
            for (int i = 0; i < ppt.Slides.Count; i++)
            {
                for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)
                {
                    if (ppt.Slides[i].Shapes[j] is IAutoShape)
                    {
                        IAutoShape shape = ppt.Slides[i].Shapes[j] as IAutoShape;
                        if (shape.TextFrame != null)
                        {
                            foreach (TextParagraph tp in shape.TextFrame.Paragraphs)
                            {
                                sb.Append(tp.Text + Environment.NewLine);
                            }
                        }

                    }
                }
            }


            var text = sb.ToString();
        }
    }
}
